import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Router, NavigationEnd, NavigationStart, RoutesRecognized } from '@angular/router';
import { filter } from 'rxjs/operators';
import { BullySubjectService, Bully } from './shared/service';
import { StompClientService } from './service/web-socket.service';
import { SYSTEM_EVENT } from './app.constants';
import { LoginService } from './auth/login.service';

const Pace = require('pace');

@Component({
    selector: 'itmp-root',
    template: `<router-outlet></router-outlet>`
})
export class AppComponent implements OnInit {
    Pace = Pace;

    constructor(
        private translate: TranslateService,
        private router: Router,
        private bully: BullySubjectService,
        private stomp: StompClientService,
        private ls: LoginService
    ) {
        // 添加语言支持
        this.translate.addLangs(['en', 'zh', 'vi']);
        // 设置默认语言, 一般在无法匹配的时候使用
        this.translate.setDefaultLang('en');
        // this.systemService.langSet();
        // 判断本地token是否需要用户登录
        if (!this.ls.autoAccessToken()) {
            console.warn('token failure, please login!');
        }
        // pace进度配置
        this._setPace();
    }

    // 配置Pace
    private _setPace() {
        this.Pace.options = {
            ajax: {
                trackMethods: ['GET', 'POST']
            },
            document: false,
            eventLag: true,
            elements: true,
            // Only show the progress on regular and ajax-y page navigation,
            // not every request
            restartOnRequestAfter: true,
            restartOnPushState: true
        };

        this.router.events.subscribe(event => {
            if (event instanceof NavigationStart) {
                this.Pace.start();
            }
            if (event instanceof RoutesRecognized) {
                this.Pace.restart();
            }
        });
    }

    // 路由跳转事件
    private _routerChangeHandle() {
        this.router.events.pipe(filter(e => e instanceof NavigationEnd)).subscribe(e => {
            const bully: Bully = {
                type: SYSTEM_EVENT.ROUTE_EVENT,
                data: e['urlAfterRedirects']
            };
            this.bully.setSubject(bully);
        });
    }

    // 接受ActiveMq发过来的消息
    private _createActiveMqClient() {
        // alert
        this.stomp
            .createClient('ALERT_TOPIC')
            .pipe(filter(res => !!res && res.length > 50))
            .subscribe(
                res => {
                    const data = JSON.parse(res);
                    data.type = '0';
                    const bully: Bully = {
                        type: SYSTEM_EVENT.ALERT_EVENT,
                        data
                    };
                    this.bully.setSubject(bully);
                },
                err => {}
            );
        // alert_count
        this.stomp
            .createClient('ALERT_TOTAL')
            .pipe(filter(res => !!res))
            .subscribe(
                res => {
                    const data = res;
                    const bully: Bully = {
                        type: SYSTEM_EVENT.ALERT_TOTAL,
                        data
                    };
                    this.bully.setSubject(bully);
                },
                err => {}
            );

        // alarm
        this.stomp
            .createClient('ALARM_TOPIC')
            .pipe(filter(res => !!res))
            .subscribe(
                res => {
                    const data = JSON.parse(res);
                    const bully: Bully = {
                        type: SYSTEM_EVENT.ALARM_EVENT,
                        data
                    };
                    this.bully.setSubject(bully);
                },
                err => {}
            );
        // alarm_count
        this.stomp
            .createClient('ALARM_TOTAL')
            .pipe(filter(res => !!res))
            .subscribe(
                res => {
                    const data = JSON.parse(res);
                    const bully: Bully = {
                        type: SYSTEM_EVENT.ALARM_TOTAL,
                        data
                    };
                    this.bully.setSubject(bully);
                },
                err => {}
            );
        // incident
        this.stomp
            .createClient('Topic_IR_Count')
            .pipe(filter(res => !!res))
            .subscribe(
                res => {
                    const data = JSON.parse(res);
                    const bully: Bully = {
                        type: SYSTEM_EVENT.INCIDENT_EVENT,
                        data
                    };
                    this.bully.setSubject(bully);
                },
                err => {}
            );
    }

    ngOnInit() {
        // 接受ActiveMq发过来的消息
        this._createActiveMqClient();

        this._routerChangeHandle();
    }
}
